﻿/*!	@file	r_window_win32.cpp
	@brief	ゲームウィンドウ(Win32基底
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_window_win32.h"

#if R_TARGET_COMMON_WINDOWS
//#if !defined(RDEBUG_DEVELOP_BUILD)

#include "../../r_debug.h"

cRWindowWin32 :: cRWindowWin32(void)
:
appName(_RST("R4 futhark"))
{
	windowWidth		= 1280;
	windowHeight	= 720;
}

cRWindowWin32 :: ~cRWindowWin32(void)
{
}

void	cRWindowWin32 :: setWindowTitle(const r_cstr title)
{
	::SetWindowText(hwnd, title);
}

void*	 cRWindowWin32 :: getWindowhandle(void)const
{
	return hwnd;
}
void*	 cRWindowWin32 :: getDeviceContext(void)const
{
	return hdc;
}
r_bool	 cRWindowWin32 :: getHasFocus(void)const
{
	return ::GetFocus() == hwnd;
}

void	cRWindowWin32 :: setAppName(const r_cstr name)
{
	appName = name;
}

void		cRWindowWin32 :: setWindowSize(r_si32 width, r_si32 height)
{
	windowWidth = width;
	windowHeight = height;
}

void		cRWindowWin32 :: setIsFullScreen(r_bool fullscreen)
{
	isFullScreen = fullscreen;
}

r_si32 cRWindowWin32 :: initWindows(void)
{
	hInst = static_cast<HINSTANCE>(::GetModuleHandle(NULL));
	WNDCLASSEX wc;
	wc.style = CS_HREDRAW | CS_VREDRAW;										// ウィンドウスタイル
	wc.lpfnWndProc = funcWndProc;											// ウインドウ関数
	wc.cbClsExtra = 0;														// エキストラなし
	wc.cbWndExtra = 0;														// エキストラなし
	wc.cbSize = sizeof(WNDCLASSEX);											// WNDCLASSEX構造体のサイズを設定
	wc.hInstance = hInst;													// このインスタンスへのハンドル
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);								// ラージアイコン
	wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO);								// スモールアイコン
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);								// カーソルスタイル
	wc.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH);				// 背景色
	wc.lpszMenuName = NULL;													// メニュー
	wc.lpszClassName = appName;												// ウインドウクラス名

	::RegisterClassEx(&wc);

	RECT windowRect;
	::SetRect(&windowRect, 0, 0, windowWidth, windowHeight);

	::AdjustWindowRect(&windowRect, isFullScreen ? WS_POPUP : WS_SYSMENU | WS_CAPTION, false);

	r_si32 win_w = windowRect.right - windowRect.left;
	r_si32 win_h = windowRect.bottom - windowRect.top;
	r_si32 win_x = (::GetSystemMetrics(0) >> 1) - (win_w >> 1);
	r_si32 win_y = (::GetSystemMetrics(1) >> 1) - (win_h >> 1);
	hwnd = ::CreateWindow(appName,		// ウインドウクラス名
	appName,							// タイトルバーの標題
	(isFullScreen ? WS_POPUP : WS_SYSMENU) | WS_MINIMIZEBOX,				// ウインドウスタイル
	isFullScreen ? 0 : win_x, isFullScreen ? 0 : win_y,						// X, Y座標
	win_w, win_h,															// 幅, 高さ
	HWND_DESKTOP,															// 親ウインドウ
	NULL,																	// メニュー
	hInst,																	// このプログラムのインスタンスのハンドル
	NULL																	// 追加引数
	);

	hdc = ::GetDC(hwnd);
	return 0;
}


LRESULT CALLBACK  cRWindowWin32 :: funcWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return(::DefWindowProc(hWnd, uMsg, wParam, lParam));
	}
	return(0L);
}

//#endif // !defined(RDEBUG_DEVELOP_BUILD)
#endif // R_TARGET_COMMON_WINDOWS
